package com.compomics.prioritizer.general.gui;

import com.compomics.prioritizer.db.accessors.User;

import javax.swing.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 21-nov-2008
 * Time: 8:40:23
 * To change this template use File | Settings | File Templates.
 */
public class AddUserGui extends JFrame {
    private JPanel jContentPane;
    private JComboBox comboBox1;
    private JTextField txtUserName;
    private JTextField txtUserMail;
    private JButton storeButton;
    private Connection iConn;


    public AddUserGui(Connection aConn) {
        super("Create a new user");
        this.iConn = aConn;


        $$$setupUI$$$();
        storeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                storeNewUser();
            }
        });

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screen.width / 5), (screen.height / 10));
        setContentPane(jContentPane);
        this.setSize(400, 400);
        this.setVisible(true);
    }


    public void storeNewUser() {
        int role = comboBox1.getSelectedIndex() + 1;
        String userName = txtUserName.getText();
        String userMail = txtUserMail.getText();

        HashMap hm = new HashMap();
        hm.put(User.NAME, userName);
        hm.put(User.E_MAIL, userMail);
        hm.put(User.ROLE, role);

        User user = new User(hm);
        try {
            user.persist(iConn);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to store new user:  ", e.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JOptionPane.showMessageDialog(this, "Stored new user", "Great succes!", JOptionPane.INFORMATION_MESSAGE);
        this.setVisible(false);
    }

    private void createUIComponents() {
        String[] userRoles = {"Submitting User", "Principal Investigator", "Responsible Staff", "Administrator"};
        comboBox1 = new JComboBox(userRoles);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jContentPane = new JPanel();
        jContentPane.setLayout(new GridBagLayout());
        final JLabel label1 = new JLabel();
        label1.setText("Username:");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jContentPane.add(label1, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(comboBox1, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("E-mail:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jContentPane.add(label2, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("User role");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jContentPane.add(label3, gbc);
        txtUserName = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(txtUserName, gbc);
        txtUserMail = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(txtUserMail, gbc);
        storeButton = new JButton();
        storeButton.setText("Store");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(storeButton, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jContentPane;
    }
}
